package com.me.libgdx_test;

import java.util.ArrayList;
import java.util.Random;

public class ClassCustomMaps {

	int[][] map_list, map_levels;
	ClassPlaceObjects placement_holder;
	private Random random = new Random(System.currentTimeMillis());
	private ClassCharacterPlayer player;
	ClassCustomMaps(ClassPlaceObjects placement_holder, ClassCharacterPlayer player, int[][] map_list, int[][] map_levels){
		this.placement_holder = placement_holder;
		this.map_list = map_list;
		this.map_levels = map_levels;
		this.player = player;
		construct_map();
	}
	//% - 2 => Gold
	//x - 3 => Enemy
	//S - 4 => Shop
	//G - 5 => Glyph
	//A - 6 => Altar
	public void construct_map(){
		ClassObjectPickupDungeon object_holder;
		ClassObjectShopDungeon shop_holder;
		ClassObjectSpellDungeon spell_holder;
		ClassObjectGodDungeon god_holder;
		ClassCharacterEnemy enemy_to_place;
		
		int[] selectable_enemies = placement_holder.get_game_vars().levelObjects[placement_holder.get_game_vars().player_level].monsters;
		
		ArrayList<ClassDungeonSpell> useable_spells = new ArrayList<ClassDungeonSpell>();
		for (ClassDungeonSpell spell:placement_holder.get_game_vars().spellObjects){
			useable_spells.add(spell);
		}
		
		for(int y = 0; y < map_list[0].length;y++){
			for(int x = 0; x < map_list.length;x++){
				Point location = new Point(x,y);
				switch(map_list[x][y]){
					//pickup
					case 2:
						object_holder = new ClassObjectPickupDungeon(placement_holder.get_game_vars().iconObjects[map_levels[x][y]]);
						object_holder.set_location(location);
						placement_holder.get_pickups().add(object_holder);						
						placement_holder.get_placed_things().add(location);
						break;
					//enemy
					case 3:
						enemy_to_place = new ClassCharacterEnemy(placement_holder.get_game_vars().enemyObjects[selectable_enemies[random.nextInt( 4 )]]);
						enemy_to_place.set_location(location);
						enemy_to_place.set_concrete_level(map_levels[x][y],placement_holder.get_game_vars().levelObjects[placement_holder.get_game_vars().player_level].difficulty);
						enemy_to_place.set_modifiers(placement_holder.get_game_vars().modObjects);
						player.get_class_mods().playerEnemyMod.set_enemy(enemy_to_place);
						placement_holder.get_enemies().add(enemy_to_place);						
						placement_holder.get_placed_things().add(location);
						break;
					//Shop
					case 4:
						ArrayList<ClassDungeonShop> shop_items = new ArrayList<ClassDungeonShop>();
						ArrayList<ClassDungeonShop> upgradeable_used = new ArrayList<ClassDungeonShop>();
						while(shop_items.size()<3){
							ClassDungeonShop temp_holder = placement_holder.get_game_vars().shopObjects[random.nextInt(placement_holder.get_game_vars().shopObjects.length)];
							while(!temp_holder.actual_shop || (temp_holder.upgradeable && upgradeable_used.contains(temp_holder))){
								temp_holder = placement_holder.get_game_vars().shopObjects[random.nextInt(placement_holder.get_game_vars().shopObjects.length)];
							}
							if(temp_holder.upgradeable){upgradeable_used.add(temp_holder);}
							shop_items.add(new ClassDungeonShop(temp_holder));
						}
						shop_holder = new ClassObjectShopDungeon(shop_items,2,placement_holder.get_shop_discount());
						shop_holder.set_location(location);
						placement_holder.get_shops().add(shop_holder);						
						placement_holder.get_placed_things().add(location);
						break;
					//Glyph
					case 5:
						spell_holder = new ClassObjectSpellDungeon(useable_spells.get(map_levels[x][y]));
						spell_holder.set_location(location);
						placement_holder.get_spells().add(spell_holder);						
						placement_holder.get_placed_things().add(location);
						break;
					//Altar
					case 6:
						ClassDungeonGod holder = placement_holder.get_game_vars().godObjects[map_levels[x][y]];
						holder.placed_altar();
						god_holder = new ClassObjectGodDungeon(holder,placement_holder.get_game_vars().get_god_map());
						god_holder.set_location(location);
						placement_holder.get_gods().add(god_holder);						
						placement_holder.get_placed_things().add(location);
						break;
				}
			}
		}
	}
}
